<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/10
 * Time: 13:02
 */
header('Content-Type:application/json');
include '../classes/logincheck.php';
include '../classes/conn.php';
if(islogin()=="teacher_login")
{
    $sections=array();
    $sub_sections=array();
    $query1="select num,name,taken from section where class='{$_POST['class_num']}' and teacher='{$_SESSION['t_num']}'";
    $res=mysql_query($query1);
    if($res){
        while ($row=mysql_fetch_assoc($res)){
            $sections[]=$row;
        }
        for($i=0;$i<count($sections);$i++){

            $query2="select num,name,section,taken from subsection where  
class='{$_POST['class_num']}' and teacher='{$_SESSION['t_num']}' and section='{$sections[$i]['num']}'";

            $res=mysql_query($query2);
            if($res){
                while ($row=mysql_fetch_assoc($res)){
                    $sub_sections[]=$row;
                }
            }
        }
        for($i=0;$i<count($sections);$i++){
            for($j=0;$j<count($sub_sections);$j++){
                if($sections[$i]['num']==$sub_sections[$j]['section']){
                    $sections[$i]['sub'][]=$sub_sections[$j];
                }
            }
        }
        $data=$sections;
        $data=json_encode($data);
        echo $data;
    }
}
